<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

class plugin_erling_grid_ad {
    public function global_header() {
        global $_G;
        $grid_columns = C::t('#erling_gridad_combo#configs')->fetch_by_key('grid_columns');
        $grid_rows = C::t('#erling_gridad_combo#configs')->fetch_by_key('grid_rows');
        $grid_columns = $grid_columns ? intval($grid_columns['value']) : 3;
        $grid_rows = $grid_rows ? intval($grid_rows['value']) : 3;
        
        $header_ads = C::t('#erling_gridad_combo#ad')->fetch_all(1, 5, $_G['uid'], 2, null, 1);
        
        // 处理网格数据
        $grid_items = array();
        $total_slots = $grid_columns * $grid_rows;
        for ($i = 1; $i <= $total_slots; $i++) {
            $row = ceil($i / $grid_columns);
            $col = $i % $grid_columns;
            $col = $col == 0 ? $grid_columns : $col;
            $grid_items[$i] = array(
                'ad' => null,
                'grid_row' => $row,
                'grid_col' => $col,
            );
        }
        
        foreach ($header_ads as $ad) {
            $sort = intval($ad['sort']);
            if (isset($grid_items[$sort])) {
                
                $grid_items[$sort]['ad'] = $ad;
            }
        }
        
        $ad_html = '';
        ob_start();
        $_var['grid_items'] = $grid_items;
        $_var['grid_columns'] = $grid_columns;
        $_var['grid_rows'] = $grid_rows;
        $_var['empty_text'] = C::t('#erling_gridad_combo#configs')->fetch_by_key('empty_text')['value'] ??'广告位招租中';
        $_var['default_color'] = C::t('#erling_gridad_combo#configs')->fetch_by_key('default_color')['value']??'#333333';
        $_var['default_bgcolor'] = C::t('#erling_gridad_combo#configs')->fetch_by_key('default_bgcolor')['value']??'#FFFFFF';
        $position = 2;

        include template('erling_gridad_combo:ad_display');
        $ad_html = ob_get_contents();
        ob_end_clean();

        return $ad_html;
    }
    
    
}